package com.atguigu.spzx.manager.mapper;
import org.apache.ibatis.annotations.Param;

import com.atguigu.spzx.manager.entity.SysUserRole;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * SysUserRoleMapper
 * </p>
 *
 * @author flyduck
 * @since 2023/11/2
 */
@Mapper
public interface SysUserRoleMapper extends BaseMapper<SysUserRole> {

    int insertList(@Param("list")List<SysUserRole> list);

    /**
     * 根据用户id查询
     * @return
     */
    default List<SysUserRole> findByUserId(Long userId) {
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        return this.selectList(queryWrapper);
    }

    /**
     * 根据用户id删除
     * @param userId
     */
    default void deleteByUserId(Long userId) {
        QueryWrapper<SysUserRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        this.delete(queryWrapper);
    }

    /**
     * 批量保存
     * @param userId
     * @param roleIdList
     */
    default void insertList(Long userId, List<Long> roleIdList) {
        List<SysUserRole> list = new ArrayList<>();
        for (Long roleId : roleIdList) {
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setUserId(userId);
            sysUserRole.setRoleId(roleId);
            list.add(sysUserRole);
        }
        this.insertList(list);
    }
}
